function toggleFilter(field) {
        if ($('#cb_'+field).attr('checked')) {
            $('.ob_'+field).show();
            toggleOperator(field);
        } else {
            $('.ob_'+field).hide();
        }
    }

    function toggleOperator(field) {
        var operator = $("#operators_" + field).val();
        switch (operator) {
        case "!*":
        case "*":
        case "t":
        case "w":
        case "o":
        case "c":
          $("#div_values_" + field).hide();
            break;
        default:
          $("#div_values_" + field).show();
          break;
    }
    }

    function toggleMultiSelect(field) {
        if ($('#values_'+field).attr('multiple'))
            $('#values_'+field).attr('multiple', false);
        else
            $('#values_'+field).attr('multiple', true);
    }

    function moveOptionUp(selectID){
        $('#'+selectID+' option:selected').each(function(){
            $(this).insertBefore($(this).prev());
        });
    }

    function moveOptionDown(selectID){
        $('#'+selectID+' option:selected').each(function(){
            $(this).insertAfter($(this).next());
        });
    }

    function submitFilter() {
        $("#selected_columns").each(function(){
            $("#selected_columns option").attr("selected","selected");
        });

        $('#admin_listing_filter').submit();
    }

    $(function() {
        $("#move_right,#move_left").click(function(event) {
            //var id = $(event.target).attr("id");
            var id = $(this).attr("id");
            var selectFrom = id == "move_right" ? "#available_columns" : "#selected_columns";
            var moveTo = id == "move_right" ? "#selected_columns" : "#available_columns";

            var output = [];
            var selectedItems = $(selectFrom + " option:selected");
            $.each(selectedItems, function(key, e) {
                output.push('<option value="' + e.value + '">' + e.text + '</option>');
            });

            $(moveTo).append(output.join(""));
            selectedItems.remove();

            return false;
        });

        $('input:checkbox').each(function(){ toggleFilter($(this).val()); });
        $('#filter_btn').click(function(){
            if ($(this).hasClass('filtre_icon_select')) {
                $(this).removeClass('filtre_icon_select');
                $('#filter_block').fadeIn();
            } else {
                $(this).addClass('filtre_icon_select');
                $('#filter_block').fadeOut();
            }
            return false;
        });

        $('#options_btn').click(function(){
            if ($(this).hasClass('filtre_icon_select')) {
                $(this).removeClass('filtre_icon_select');
                $('#options_block').fadeIn();
            } else {
                $(this).addClass('filtre_icon_select');
                $('#options_block').fadeOut();
            }
            return false;
        });

        $('#move_up').click(function(){
            moveOptionUp('selected_columns');
            return false;
        });

        $('#move_down').click(function(){
            moveOptionDown('selected_columns');
            return false;
        });

        $('#filter_submit_btn').click(function(){ submitFilter(); return false; });
        $('#filter_cancel_btn').click(function(){
            $('#reset_filter').val(2);
            $('#admin_listing_filter').submit();
            return false;
        });

        $('#quick_action_btn').click(function() {
            if ($('#quick_actions').val() != '') {
                $('#quick_action').val($('#quick_actions').val());
                $('#admin_listing_frm').submit();
            }

            return false;
        });
    });